package com.test.domain;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;

public class MemberDAO {

	private SessionFactory sessionFactory = null;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	// 查詢all HQL語法
	public void selectAll() {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from MemberBean");
		List<?> users = query.list();
		Iterator list = users.iterator();
		while (list.hasNext()) {
			MemberBean item = (MemberBean) list.next();
			System.out.println(item.getSex());
			System.out.println(item.getAccount());
		}
		session.close();
		System.out.println("done.");
	}


	// 查詢by ID
	public void selectById(int memId) {
		//Session session = DefaultFactory.getSessionFactory().openSession();
		Session session =sessionFactory.openSession();
		Query query = session
				.createQuery("from MemberBean where memId = :memyaId ");
		query.setInteger("memyaId", memId);
		List<?> users = query.list();
		Iterator list = users.iterator();
		while (list.hasNext()) {
			MemberBean item = (MemberBean) list.next();
			System.out.println(item.getSex());
			System.out.println(item.getAccount());
		}
		session.close();
		System.out.println("done.");
	}

	// 刪除
	@Transactional
	public void delete(int memId) {
		//Session session = DefaultFactory.getSessionFactory().openSession();
		Session session = sessionFactory.openSession();
		//Transaction tx = session.beginTransaction(); // 要寫交易才會開始
		Query query = session
				.createQuery("delete MemberBean where memId = :memyaId ");
		query.setInteger("memyaId", memId);
		MemberBean bean = new MemberBean();
		
		int result = query.executeUpdate();
		session.delete(memId);
		//tx.commit();
		session.close();
		System.out.println(" done.");
	}
	
	@Transactional
	public int insert() {
		//Session session = DefaultFactory.getSessionFactory().openSession();
		Session session = sessionFactory.openSession();
		//Transaction tx = session.beginTransaction();
		MemberBean bean = new MemberBean();
		bean.setAccount("Q");
		bean.setName("C");
		bean.setMemPassword("F");
		bean.setBirth(new java.util.Date());
		bean.setEmail("AA");
		bean.setSex("男");
		// 無圖片
		bean.setBirth(new java.util.Date());
		bean.setPhone("023232323");
		bean.setMemAddress("台北市");
		bean.setMemtypeId(1);
		bean.setMemstatusId(2);
		bean.setMemDate(new Timestamp(System.currentTimeMillis()));

		int result=(Integer) session.save(bean);
		//tx.commit();
		session.close();
		return  result;
	}
	
	@Transactional
	public void update() {
		//Session session = DefaultFactory.getSessionFactory().openSession();
		Session session = sessionFactory.openSession();
		//Transaction tx = session.beginTransaction();
		MemberBean bean = new MemberBean();
		bean.setMemId(5);
		bean.setAccount("S");
		bean.setName("C");
		bean.setMemPassword("F");
		bean.setBirth(new java.util.Date());
		bean.setEmail("AA");
		bean.setSex("無");
		// 無圖片
		bean.setBirth(new java.util.Date());
		bean.setPhone("023232323");
		bean.setMemAddress("台北市");
		bean.setMemtypeId(1);
		bean.setMemstatusId(2);
		bean.setMemDate(new Timestamp(System.currentTimeMillis()));

		session.update(bean);
		//tx.commit();
		//session.close();
	}

	public static void main(String[] args) {

		// selectAll();
		// selectById(1);
		// delete(2);
		// insert();
		// update();
	}

}
